<main class="" th:fragment="commodityAdmin">
    <body>
    <!-- 为ECharts准备一个具备大小（宽高）的Dom -->
    <div id="toolbar">
        <div id="tb" style="padding:3px">

            <span>商品ID:</span>
            <input id="search_id" style="line-height:26px;border:1px solid #ccc">
            <span>商品昵称:</span>
            <input id="search_name" style="line-height:26px;border:1px solid #ccc">
            <a href="#" class="easyui-linkbutton" plain="true" onclick="doSearch()" style="background: #ffffff">搜索</a>
        </div>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true"
           style="background: #ffffff" onclick="newcommodity()">添加商品</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true"
           style="background: #ffffff" onclick="editcommodity()">修改商品</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true"
           style="background: #ffffff" onclick="destroycommodity()">删除商品</a>
    </div>
    <table id="dg" class="easyui-datagrid" style="width:1280px;height:500px"></table>

    <div id="dlg" class="easyui-dialog" style="width:550px"
         data-options="closed:true,modal:true,border:'thin',buttons:'#dlg-buttons'">
        <form id="fm" method="post" novalidate style="margin:0;padding:20px 50px">
            <h3>商品信息</h3>
            <div style="margin-bottom:10px">
                <input name="name" class="easyui-textbox" required="true" label="商品昵称:" style="width:100%">
            </div>
            <div style="margin-bottom:10px">
                <input class="easyui-combobox" name="supplierId" label="商家信息:" style="width:100%"
                       data-options="valueField:'id',textField:'name',url:'/supplier/select'">
            </div>

            <div style="margin-bottom:10px">
                <input class="easyui-combobox" name="warehouseId" label="仓库信息:" style="width:100%"
                       data-options="valueField:'id',textField:'name',url:'/warehouse/select'">
            </div>

            <div style="margin-bottom:10px">
                <input class="easyui-datebox" name="saveTime" label="生产日期:" style="width:100%">
            </div>

            <div style="margin-bottom:10px">
                <input name="warrantyTime" class="easyui-numberbox" required="true" label="保质期:" style="width:100%">
            </div>

            <div style="margin-bottom:10px">
                <input name="remarks" class="easyui-textbox" required="true" label="备注:" style="width:100%">
            </div>


        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="savecommodity()"
           style="width:90px">Save</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel"
           onclick="javascript:$('#dlg').dialog('close')" style="width:90px">Cancel</a>
    </div>
    <script type="text/javascript" th:inline="none">
        var url;
        install();

        function install() {
            $('#dg').datagrid({
                url: '/commodity/s',
                pagination: "true",
                rownumbers: "true",
                fitColumns: "true",
                singleSelect: "true",
                title: "商品管理",
                columns: [[
                    {field: 'id', title: '商品ID', width: 50},
                    {field: 'name', title: '商品昵称', width: 50},
                    {field: 'warehouseName', title: '仓库ID', width: 50},
                    {field: 'supplierName', title: '供应商ID', width: 50},
                    {field: 'remarks', title: '备注', width: 50},
                    {field: 'stock', title: '库存', width: 50},
                    {field: 'purchase', title: '采购数', width: 50},
                    {field: 'warrantyTime', title: '保质期', width: 50},
                    {field: 'saveTime', title: '录入时间', width: 50},
                    {field: 'state', title: '状态', width: 50,styler: function (value, row, index) {
                            if (value && value == '过期') {
                                return 'background-color:#ffee00;color:red;';
                                // the function can return predefined css class and inline style
                                // return {class:'c1',style:'color:red'}
                            }
                        }},
                    {
                        field: 'stockState', title: '库存状态', width: 50, styler: function (value, row, index) {
                            if (value && value != '') {
                                return 'background-color:#ffee00;color:red;';
                                // the function can return predefined css class and inline style
                                // return {class:'c1',style:'color:red'}
                            }
                        }
                    },
                ]]
            });
        }

        //搜索
        function doSearch() {
            var data = {};
            if ($('#search_name').val()) {
                data.name = $('#search_name').val();
            }
            if ($('#search_id').val()) {
                data.id = $('#search_id').val();
            }
            $('#dg').datagrid('load', data);
        }

        //添加
        function newcommodity() {
            $('#dlg').dialog('open').dialog('center').dialog('setTitle', '新增商品');
            $('#fm').form('clear');
            url = '/commodity/add';
        }

        //修改
        function editcommodity() {
            var row = $('#dg').datagrid('getSelected');
            if(row.examineState == '待审核'){
                alert('当前商品不允许操作');
                return ;
            }
            if (row) {
                $('#dlg').dialog('open').dialog('center').dialog('setTitle', '修改商品');
                $('#fm').form('load', row);
                url = '/commodity/up?id=' + row.id;
            }
        }

        //添加和修改保存商品
        function savecommodity() {

            $('#fm').form('submit', {
                url: url,
                onSubmit: function () {
                    return $(this).form('validate');
                },
                success: function (result) {
                    if (result == 'false-has-double') {
                        $.messager.show({
                            title: 'Error',
                            msg: '重名'
                        });
                    } else {
                        var result = eval('(' + result + ')');
                        $('#dlg').dialog('close');        // close the dialog
                        $('#dg').datagrid('reload');    // reload the warehouse data
                    }
                }
            });
        }

        //删除
        function destroycommodity() {
            var row = $('#dg').datagrid('getSelected');
            if(row.examineState == '待审核'){
                alert('当前商品不允许操作');
                return ;
            }
            if (row.stock > 0) {
                alert("商品库存不为0,无法删除");
                return;
            }
            if (row) {
                $.messager.confirm('Confirm', '确定要销毁此商品吗?', function (r) {
                    if (r) {
                        $.post('/commodity/del', {id: row.id}, function (result) {
                            if (result) {
                                //echarsFunction();
                                $('#dg').datagrid('reload');    // reload the commodity data
                            } else {
                                $.messager.show({    // show error message
                                    title: 'Error',
                                    msg: '不允许删除，存在当前商品库存'
                                });
                            }
                        }, 'json');
                    }
                });
            }
        }
    </script>
    </body>
</main>